package com.etbsms.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.etbsms.form.UsersForm;


@Entity
@Table(name = "Etbsms_User")
public class Users implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6387873813732155225L;
	
	public Users(){}
	
	public Users(UsersForm form){
		this.setEmail(form.getEmail());
		this.setFullname(form.getFullname());
		this.setPassword(form.getPassword());
		this.setUsername(form.getUsername());
	}
	
	public void update(UsersForm form){
		this.email = form.getEmail();
		this.fullname = form.getFullname();
		this.row_per_page = form.getRow_per_page();
	}
	
	private int seq_no;
	private String username;
	private String password;
	private String fullname;
	private String email;	
	private boolean enabled;
	private Date created;
	private Role current_role;
	private String role_name;
	private int row_per_page = 10;
//	private Role role;
	private Set<Role> role = new HashSet<Role>();
	private Map<Role,Boolean> roleMap = new HashMap<Role,Boolean>();

	@Id 
	@Column(name = "seq_no" ,nullable = false)
	@GeneratedValue(generator = "paymentableGenerator")       
	@GenericGenerator(name = "paymentableGenerator", strategy = "identity")  
	public int getSeq_no() {
		return seq_no;
	}
	public void setSeq_no(int seq_no) {
		this.seq_no = seq_no;
	}
	
	@Column(name = "username" ,nullable = false,length = 64)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "password" ,nullable = false,length = 64)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name = "fullname" ,nullable = true,length = 64)
	public String getFullname() {
		return fullname;
	}
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	
	@Column(name = "email" ,nullable = true,length = 256)
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "enabled" ,nullable = false)
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	@Column(name = "created" ,nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	
	/*
	@ManyToOne
	@JoinColumn(name="role_seqNo",nullable=false)
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	*/
	
	@ManyToMany(cascade={CascadeType.ALL})
	@JoinTable(name="Etbsms_User_Role",
		joinColumns={@JoinColumn(name="users_uuid")},
		inverseJoinColumns={@JoinColumn(name="role_uuid")})
	public Set<Role> getRole() {
		return role;
	}
	public void setRole(Set<Role> role) {
		this.role = role;
	}

	@Transient
	public Map<Role,Boolean> getRoleMap() {
		return roleMap;
	}
	public void setRoleMap(Map<Role,Boolean> roleMap) {
		this.roleMap = roleMap;
	}

	@Transient
	public Role getCurrent_role() {
		return current_role;
	}
	public void setCurrent_role(Role current_role) {
		this.current_role = current_role;
	}
	
	@Transient
	public String getRole_name() {
		role_name = this.getCurrent_role().getName();		
		return role_name;
	}
	public void setRole_name(String role_name) {
		this.role_name = role_name;
	}

	@Column(name = "row_per_page" ,nullable = false)
	public int getRow_per_page() {
		return row_per_page;
	}
	public void setRow_per_page(int row_per_page) {
		this.row_per_page = row_per_page;
	}
}
